package ltd.followkba.page;

import android.view.View;

import com.kcbsdk.EventHandler;
import com.kcbsdk.MyWorks.AFileInfo;
import com.kcbsdk.MyWorks.AObbligInfo;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import ltd.advskin.MRAM;
import ltd.advskin.MSKIN;
import ltd.advskin.base.BaseInput;
import ltd.advskin.base.BaseList;
import ltd.advskin.base.BasePage;

import ltd.advskin.base.KcListHolder;
import ltd.advskin.base.KcListListener;
import ltd.advskin.base.KcTypeListener;
import ltd.advskin.base.MPAGE;
import ltd.advskin.task.TaskFree;
import ltd.advskin.view.KcBtnProgress;
import ltd.advskin.view.KcHeaderView;
import ltd.advskin.view.KcListView;
import ltd.followkba.MAPP;
import ltd.followkba.MDBS;
import ltd.followkba.R;
import ltd.followkba.task.BAppUser;
import ltd.followkba.task.BAppWorks;
import ltd.followkba.task.BDbsAccount;
import ltd.followkba.task.BUploadPlan;
import ltd.followkba.task.EditUtils;
import main.MAPI;

import static com.kcbsdk.wpapper.KCB_LIST_ORIGINAL_FLAG;
import static ltd.advskin.MSKIN.MSTRING;
import static ltd.advskin.MSKIN.MTOAST;
import static ltd.advskin.view.KcListView.LIST_CREATE_MAIN;
import static main.MAPI.mMyWorks;

public class UploadListPage extends BaseList {
    private List<BUploadPlan> mBUploadPlan;
    private KcBtnProgress kbtNewUpload;
    private int gWorksType;

    // 所有的页面都接受同样的方式打开LOG
    public static boolean logEnable;
    public void MLOG(String text) {
        if (logEnable) {
            MSTRING(MAPI.GET_LOG_NAME(this) + text);
        }
    }

    @Override
    public void onInitView() {                       // 使用者需要继承，初始化页面控件等显示
        setLayout(R.layout.page_upload_list);
        mKcHeaderView = (KcHeaderView) findViewById(R.id.khvUpload);
        gWorksType = MAPP.mBDbsAccount.WorksType;
        String text = String.format("上传计划列表 %s",  MAPP.mWorksName[gWorksType]);
        mKcHeaderView.setTitle(text, new KcTypeListener() {
            @Override
            public void onMessage(int type) {
                switch (type) {
                    case KcHeaderView.TYPE_CLICK_LEFT:
                        onFinish();
                        break;
                }
            }
        });
        kbtNewUpload = findViewById(R.id.kbtNewUpload);;
        kbtNewUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 新建上传计划
                BUploadPlan bUploadPlan = (BUploadPlan) v.getTag();
                MAPP.startEditUploadPage(bUploadPlan);
            }
        });
        initPageListView( 1 );
    }
    @Override
    public void onInitData(){                                                                       // onInitView结束，开始加载数据
            makeUploadList();

        mMyWorks.setUiEventCallBack(new EventHandler() {
            @Override
            public void onComplete(int methodType, boolean isFail, String message) {
                if (methodType > 0) {
                }else {
                    if (mMyWorks.isEventDataRefresh()){
                        showInfo();
                    }
                }
            }
        });
        showInfo();
    }

    @Override
    public void onPageUpdate(BasePage basePage) {    // 更新显示页面数据，用于刷新显示 basePage返回上面的页面，可以使用getPageResult()获得返回的内容
        Object object = getPageResult(basePage);
        if (object != null) {
            makeUploadList();
        }
    }

    @Override
    public void onFinish() {       // 使用者选择性继承，用户按了后退键，不继承就直接关闭了
        setClose();
    }

    private void showInfo() {
        int tabSel = getTabList();
        MLOG(String.format("showInfoA (%s)",  tabSel));
        if (tabSel > 0) {
            tabSel -= 1;
            setListView(tabSel, (List) mBUploadPlan);
        }
    }

    private void makeUploadList(){
        new TaskFree(null, new TaskFree.Listener() {
            @Override
            public void onMessage(boolean isThread, Object object) {
                if (isThread){
                    mBUploadPlan = MDBS.getBUploadPlan(MAPP.mBDbsAccount);
                    int length = (mBUploadPlan != null) ? mBUploadPlan.size() : 0;
                    if (length > 0) {
                        Collections.sort(mBUploadPlan, new Comparator<BUploadPlan>() {
                            public int compare(BUploadPlan info1, BUploadPlan info2) {
                                return (info2.PlanId - info1.PlanId);  // 降序
                            }
                        });
                    }
                    return;
                }
                showInfo();
            }
        });
    }
//    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//    // KcListView的全部处理，放在页面的最后位置统一处理 ///////////////////////////////////////////////////////////////////
//    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    private static final int[] item_upload_plan = new int[]{
            R.layout.item_upload_plan, 9,
            R.id.kbiHeadPlan, R.id.kbtWorksPlan, R.id.tvDonePlan,
            R.id.kbtNamePlan, R.id.kbtStatePlan,
            R.id.kbtTimePlan, R.id.kbtDelPlan,
            R.id.kbtIntroPlan,
            R.id.kbtRetryPlan,
    };
    private void initListView(int tab, KcListView kcListView) {
        mKcListView[tab] = kcListView;
        mKcListView[tab].initView(null, item_upload_plan, null, new KcListListener() {
            @Override
            public int onMessage(int type, int tab, Object object) {
                switch (type) {
                    case LIST_CREATE_MAIN:

                        break;
                }
                return 0;
            }
            @Override
            public int onBindItemHolder(boolean skipImage, int tab, int position, final KcListHolder holder) {
                BUploadPlan bUploadPlan = (BUploadPlan)getListView(tab, position);
                MSKIN.showWorksCover(bUploadPlan.CoverFile, holder.mView[0]);
                MAPP.showWorksName(bUploadPlan.WorksName, gWorksType, bUploadPlan.IsOriginal, bUploadPlan.IsVideo, holder.mView[1]);
                MLOG(String.format("UploadA aPhotoInfo B %s(%s)%s", position, bUploadPlan.WorksName ,  bUploadPlan.Intro));

                SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
                String text = (bUploadPlan.DoneTick > 0) ? String.format("%s已上传", sdf.format(bUploadPlan.DoneTick)) : String.format("未上传");
                MSKIN.setText(text, holder.mView[2]);
//
                MSKIN.setText(String.format("%s：%s",  MAPP.mWorksName[bUploadPlan.AppType], bUploadPlan.UserName), holder.mView[3]);
//
                text = String.format("上传时间：%s", sdf.format(bUploadPlan.SetTick));
                MSKIN.setText(text, holder.mView[5]);
                holder.mView[6].setTag(bUploadPlan);
                holder.mView[6].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {                               // 删除上传任务
                        BUploadPlan bUploadPlan = (BUploadPlan) v.getTag();
                        MDBS.delBUploadPlan(MAPP.mBDbsAccount, bUploadPlan);
                        makeUploadList();
                        MTOAST(String.format("已删除上传计划 %s %s", bUploadPlan.UserName, bUploadPlan.WorksName));
                    }
                });
                MSKIN.setText(bUploadPlan.Intro, holder.mView[7]);
                if (bUploadPlan.Retry != null){
                    int length = bUploadPlan.Retry.size();
                    text = "完成\n";
                    for (int index = 0; index < length; index++){
                        text = String.format("%s_%s\n", text, bUploadPlan.Retry.get(index));
                    }
                    MSKIN.setText(text, holder.mView[8]);
                }else {
                    MSKIN.setText(" ", holder.mView[8]);
                }
                holder.mItemView.setTag(bUploadPlan);
                holder.mItemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BUploadPlan bUploadPlan = (BUploadPlan) v.getTag();
                        EditUploadPage editUploadPage = new EditUploadPage();
                        editUploadPage.mBaseInput = new BaseInput(bUploadPlan);
                        MPAGE.startBasePage(editUploadPage);
                    }
                });
                return 0;
            }
        });
        // (d)列表最后更新日期时间 (n)下次更新日期时间 (c)当前数量 (q)列表最大的数量
        mKcListView[tab].setStatus(false, true, "", "还没有上传计划", null);
        mKcListView[tab].setTabNumber(tab);
        mKcListView[tab].setSpaceHeight(MRAM.g4ScreenWidth);
    }

    private void initPageListView(int tabQty) {
        mKcListView = new KcListView[tabQty];
        if (tabQty == 1) {
            mKcListView[0] = (KcListView) findViewById(R.id.klvUpload);
            initListView(0, mKcListView[0]);
            return;
        }
    }
}

